package com.project.tructc.MBean;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.RandomStringUtils;
import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;

import com.project.entity.Account;
import com.project.entity.Property;
import com.project.entity.Property_Image;
import com.project.util.MessageCall;


@ManagedBean(name="mPropertyImage")
@SessionScoped
public class MPropertyImage {
	

	private List propImageList;
	private List propImageListLink;
	private String propImageTempDeleteLink;
	private List prop_img;
	
	public MPropertyImage()
	{
		prop_img = new ArrayList();
		propImageList = new ArrayList();
		propImageListLink = new ArrayList();
	}
	
	
	

	public List<Property_Image> getProp_img() {
		return prop_img;
	}




	public void setProp_img(List<Property_Image> prop_img) {
		this.prop_img = prop_img;
	}




	public void setPropImageTempDeleteLink(String propImageTempDeleteLink) {
		this.propImageTempDeleteLink = propImageTempDeleteLink;
	}


	public String getPropImageTempDeleteLink() {
		return propImageTempDeleteLink;
	}


	public void setPropImageListLink(ArrayList propImageListLink) {
		this.propImageListLink = propImageListLink;
	}


	public List getPropImageListLink() {
		return propImageListLink;
	}


	public void setPropImageList(List propImageList) {
		this.propImageList = propImageList;
	}


	public List getPropImageList() {
		return propImageList;
	}
	
	


	@SuppressWarnings("unchecked")
	public void handleFileUpload(FileUploadEvent event) throws NamingException,
	IOException {
		
		if(this.propImageList.size() < 6)
		{
			ExternalContext extContext = FacesContext.getCurrentInstance()
			.getExternalContext();
			
			String random_number = RandomStringUtils.randomNumeric(12);
			File result = new File(extContext.getRealPath("//uploaded//PropertyImages//temp//"
					+ random_number + "_" + event.getFile().getFileName()));
			this.propImageList.add(result);
			FileOutputStream fileOutputStream = new FileOutputStream(result);
			byte[] buffer = new byte[6124];
			int bulk;
			InputStream inputStream = event.getFile().getInputstream();
			while (true) {
				bulk = inputStream.read(buffer);
				if (bulk < 0) {
					break;
				}
				fileOutputStream.write(buffer, 0, bulk);
				fileOutputStream.flush();
			}
			fileOutputStream.close();
			inputStream.close();
			
			
			this.propImageListLink.add("//uploaded//PropertyImages//temp//"
					+ random_number + "_" + event.getFile().getFileName());
		} 
		else 
		{
			MessageCall.errorMessage("Number of images limit exceed, you can upload 6 images only !", "image_limit_exceed");
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Error",
					"Number of images limit exceed, you can upload 6 images only !"));
		}
		
		

		}
	
	public int insertImagesToList(Property prop) throws NamingException
	{
		Context ctx = new InitialContext();
		
		ExternalContext extContext = FacesContext.getCurrentInstance()
		.getExternalContext();
		
		Iterator ite = this.propImageList.iterator();
		int i = 0;
		while(ite.hasNext())
		{
			File temp = (File)ite.next();
			String path = "//uploaded//PropertyImages//" + prop.getPropID() + "_"
			+ temp.getName();
		    temp.renameTo(new File(extContext.getRealPath(path)));
		    
		    Property_Image temp_img = new Property_Image();
		   
		    temp_img.setPath(path);
		    temp_img.setProperty(prop);
		    
		    this.prop_img.add(temp_img);
		    i++;
		   
		}
		this.propImageList = new ArrayList();
		this.propImageListLink = new ArrayList();
		
		return i;
	}
	
	public void deleteTempImages()
	{
		Iterator ite = this.propImageListLink.iterator();
		int i = 0;
		while(ite.hasNext())
		{
			String tempImage = (String)ite.next();
			if(this.propImageTempDeleteLink.equals(tempImage))
			{
				ite.remove();
				File temp = (File)this.propImageList.get(i);
				temp.delete();
				this.propImageList.remove(i);
			}
			i++;
		}
	}
	

}
